package com.jwdf.uda.ui.fragment.garb;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentGarbBinding;
import com.jwdf.uda.event.GuideAddEvent;
import com.jwdf.uda.event.GuideCancelEvent;
import com.jwdf.uda.event.GuideLikeListEvent;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.event.VoteAddEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.CollocationListAction;
import com.jwdf.uda.http.action.GuideLikeAction;
import com.jwdf.uda.http.action.GuideUnLikeAction;
import com.jwdf.uda.ui.activity.GuideAddActivity;
import com.jwdf.uda.ui.adapter.RecommendAdapter;
import com.jwdf.uda.ui.adapter.item.HomeRecommendItem;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.view.XListView;

import retrofit2.Response;

/**
 * Created by RD-001 on 2016/4/27.
 */
public class GarbFragment extends BaseFragment {
    private PbBean.PbSignPageFilterType mFilterType = PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_SELF;
    private FragmentGarbBinding mDataBinding;
    private RecommendAdapter mAdapter;

    public static GarbFragment instance() {
        GarbFragment view = new GarbFragment();
        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentGarbBinding.inflate(inflater, container, false);
        mDataBinding.setGarbFragment(this);
        //        mDataBinding.lyTitle.setGarbFragment(this);
        initViews();
        return mDataBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showLoadingPage(R.id.ll_loading);
    }

    private void initViews() {
        mAdapter = new RecommendAdapter(getActivity(), true);
        mDataBinding.listview.setPullRefreshEnable(true);
        mDataBinding.listview.setXListViewListener(createOnPullListener());
        mDataBinding.listview.addHeaderView(createHeaderView());
        mDataBinding.listview.setAdapter(mAdapter);
        mDataBinding.listview.setEmptyView(mDataBinding.lyEmptyContent.getRoot());
        requestData(true);

        EventBusUtil.registerEvent(this);
        EventBusUtil.registerEvent(mAdapter);
    }

    private View createHeaderView() {
        View headerView = getActivity().getLayoutInflater().inflate(R.layout.view_head_divider, null);
        return headerView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtil.unregisterEvent(this);
        EventBusUtil.unregisterEvent(mAdapter);
    }

    private XListView.IXListViewListener createOnPullListener() {
        return new XListView.IXListViewListener() {
            @Override
            public void onRefresh(XListView xListView) {
                requestData(true);
            }

            @Override
            public void onLoadMore(XListView xListView) {
                requestData(false);
            }
        };
    }
    public void onHeadActionClick(View view) {
    }
    public void onGrabAddClick(View view) {
        startActivityFromFragment(GuideAddActivity.class);
    }
    public void onEventMainThread(GuideAddEvent event) {
        requestData(true);
    }
    private void requestData(final boolean refresh) {
        //        if (refresh && mAdapter.getCount() == 0) {
        //            showProgressDialog();
        //        }

        int offset = refresh ? 0 : mAdapter.getCount();
        onNewRequestCall(CollocationListAction.newInstance(getContext(), offset, mFilterType).request(new AHttpService.IResCallback<PbBean.PbResGuideGetGuideList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideGetGuideList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    //                    closeProgressDialog();
                    closeLoadingPage();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_operation))) {
                        mDataBinding.listview.stopRefresh();
                        mDataBinding.listview.stopLoadMore();
                        return;
                    }
                    doSuccessResponse(refresh, response.body());
                }
            }
        }));
    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResGuideGetGuideList body) {
        if(body != null) {
            if(refresh) {
                mAdapter.setData(HomeRecommendItem.convert(body.getGuideListList()));
            } else {
                mAdapter.append(HomeRecommendItem.convert(body.getGuideListList()));
            }
            mDataBinding.listview.setPullLoadEnable(body.getGuideListList().size() == getPageLength());
            mDataBinding.listview.stopRefresh();
            mDataBinding.listview.stopLoadMore();
        }
    }
    public void onEventMainThread(VoteAddEvent event) {
        requestData(true);
    }

    public void onEvent(IndividualEvent event) {
        requestData(true);
    }

    public void onEventMainThread(GuideCancelEvent event) {
        requestData(true);
    }

    public void onEventMainThread(ReplyDetailEvent event){
        if (event != null && ReplyDetailEvent.EVENT_COMMENT_ADD == event.getEvent()) {
            requestData(true);
        }
    }

    public void onEventMainThread(GuideLikeListEvent event){
        switch (event.getEvent()){
            case GuideLikeListEvent.EVENT_GUIDE_LIKE:
                doLikeGuideAction(event.getmGuideId());
                break;
            case GuideLikeListEvent.EVENT_GUIDE_UNLIKE:
                doUnLikeGuideAction(event.getmGuideId());
                break;
        }
    }

        private void doLikeGuideAction(int mGuideId) {
//            showProgressDialog();
            GuideLikeAction action = GuideLikeAction.newInstance(getContext(), mGuideId);
            onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideLike>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResGuideLike> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if (getView() != null) {
//                        closeProgressDialog();
                        if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                            return;
                        }
                        doLikeGuideResponse();
                    }
                }
            }));
    }
    private void doLikeGuideResponse() {
//        mIsLike = true;
//        mIView.showPraiseView(mIsLike);
        requestData(true);
    }
    private void doUnLikeGuideAction(int mGuideId) {
//        showProgressDialog();
        GuideUnLikeAction action = GuideUnLikeAction.newInstance(getContext(), mGuideId);
        onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResGuideUnLike>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResGuideUnLike> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView() != null) {
//                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doUnLikeGuideResponse();
                }
            }
        }));
    }
    private void doUnLikeGuideResponse() {
//        mIsLike = false;
//        mIView.showPraiseView(mIsLike);
        requestData(true);
    }

}